package com.gitee.cc.system.service.impl;

import com.gitee.cc.common.utils.TreeUtil;
import com.gitee.cc.system.constant.AdminCommonConstant;
import com.gitee.cc.system.domain.Element;
import com.gitee.cc.system.domain.Menu;
import com.gitee.cc.system.domain.User;
import com.gitee.cc.system.dto.PermissionInfo;
import com.gitee.cc.system.enums.AuthExceptionEnum;
import com.gitee.cc.system.exception.AuthException;
import com.gitee.cc.system.mapper.ElementMapper;
import com.gitee.cc.system.mapper.MenuMapper;
import com.gitee.cc.system.mapper.UserMapper;
import com.gitee.cc.system.service.LoginService;
import com.gitee.cc.system.service.MenuService;
import com.gitee.cc.system.vo.FrontUser;
import com.gitee.cc.system.vo.MenuTree;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.gitee.cc.common.utils.MDUtils.MD5Encode;

/**
 * @author : cc
 * @date : Create in 13:54 2018/10/29
 */
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenuMapper menuMapper;
    @Resource
    private ElementMapper elementMapper;

    @Override
    public List<MenuTree> getMenusByUsername(User token) throws Exception {
        List<Menu> menus = menuMapper.selectAuthorityMenuByUserId(token.getId()+"");
        return getMenuTree(menus, AdminCommonConstant.ROOT);
    }

    @Override
    public List<MenuTree> getAllMenus() throws Exception {
        List<Menu> menus = menuMapper.selectAll();
        return getMenuTree(menus,AdminCommonConstant.ROOT);
    }

    @Override
    public FrontUser getFrontUser(User token) throws Exception {
        FrontUser frontUser = new FrontUser();
        BeanUtils.copyProperties(token, frontUser);
        List<Menu> menus = menuMapper.selectAuthorityMenuByUserId(token.getId()+"");
        List<PermissionInfo> menusResult = new ArrayList<>();
        List<PermissionInfo> elementsResult = new ArrayList<>();
        menu2permission(menus, menusResult);
        List<Element> elements = elementMapper.selectAuthorityElementByUserId(token.getId() + "");
        element2permission(elementsResult, elements);
        frontUser.setMenus(menusResult);
        frontUser.setElements(elementsResult);
        return frontUser;
    }

    private List<MenuTree> getMenuTree(List<Menu> menus, int root) {
        List<MenuTree> trees = new ArrayList<MenuTree>();
        MenuTree node = null;
        for (Menu menu : menus) {
            node = new MenuTree();
            BeanUtils.copyProperties(menu, node);
            trees.add(node);
        }
        return TreeUtil.bulid(trees, root);
    }
    private void menu2permission(List<Menu> menus, List<PermissionInfo> result) {
        PermissionInfo info;
        for (Menu menu : menus) {
            if (StringUtils.isBlank(menu.getHref())) {
                menu.setHref("/" + menu.getCode());
            }
            info = new PermissionInfo();
            info.setCode(menu.getCode());
            info.setType(AdminCommonConstant.RESOURCE_TYPE_MENU);
            info.setName(AdminCommonConstant.RESOURCE_ACTION_VISIT);
            String uri = menu.getHref();
            if (!uri.startsWith("/")) {
                uri = "/" + uri;
            }
            info.setUri(uri);
            info.setMethod(AdminCommonConstant.RESOURCE_REQUEST_METHOD_GET);
            result.add(info
            );
            info.setMenu(menu.getTitle());
        }
    }
    private void element2permission(List<PermissionInfo> result, List<Element> elements) {
        PermissionInfo info;
        for (Element element : elements) {
            info = new PermissionInfo();
            info.setCode(element.getCode());
            info.setType(element.getType());
            info.setUri(element.getUri());
            info.setMethod(element.getMethod());
            info.setName(element.getName());
            info.setMenu(element.getMenuId());
            result.add(info);
        }
    }
}
